using System;
using GHIElectronics.Graphics.Simple128x64;
using GHIElectronics.NETMF.FEZ;
using Microsoft.SPOT;


namespace PandaDataLogger
{
  public static class Screens
  {

    #region Screen statics
    static GPainterExtensions paint = new GPainterExtensions();
    static bool timerenabled = false;
    public static int screennumber = -1;
    static int nos = 3;
    const string c_ehome = "e-Home ";
    const string c_counters = "Counters";
    const string c_ehdl = "e-Home Data Logger";
   
    #endregion

    #region Screen Methods
    public static void ScreenRotator()
    {
      screennumber++;
      if (screennumber > nos-1)
      {
        screennumber = -1;
      //("reset screennumber" + screennumber);
        ScreenRotator();
      }

      //Debug.Print("screennumber" + screennumber);

      SelectScreen(screennumber);
    }


    public static void SelectScreen(int screen)
    {
      //Debug.Print("selectscreen " + screen);

      DisplayClear();
      switch (screen)
      {
        case 0:
          timerenabled = true;
          screen0();
          break;
        case 1:
          timerenabled = true;
          screen1();
          break;
        case 2:
          timerenabled = true;
          screen2();
          break;
        default:
          screen = 0;
          SelectScreen(0);
          break;
      }
    }

    public static void UpdateScreens(int sn)
    {
      //Debug.Print("updatescreen " + sn);
      switch (sn)
      {
        case 0:
          updatescreen0();
          break;
        case 1:
          updatescreen1();
          break;
        case 2:
          updatescreen2();
          break;
        default:
          sn = 0;
          UpdateScreens(sn);
          break;
      }
    }
    #endregion

    #region Screens

    static void screen0()
    {
      paint.Clear();
      paint.RectSE(0, 0, 127, 63);
      paint.Line(0, 14, 127, 14);
      paint.PrintCentered(0, 3, c_ehdl);
      paint.PrintCentered(0, 20, DateString(DateTime.Now));

      paint.PrintSmallRightAligned(125, 16, CounterCollection.FreeMem.ToString());

      DisplayFlush();
    }

    static void updatescreen0()
    {
      paint.PrintCentered(0, 30, TimeString(DateTime.Now));
      paint.PrintCentered(0, 40, TimeString(PandaDataLogger.Program.uptime));
      DisplayFlush();
    }

    /// <summary>
    /// Screen 0
    /// </summary>
    static void screen1()
    {
      paint.Clear();
      paint.RectSE(0, 0, 127, 63);
      paint.Line(0, 14, 127, 14);

      //Title
      paint.PrintCentered(0, 3, "e-Home Counters");

      paint.PrintSmallRightAligned(125, 16, CounterCollection.FreeMem.ToString());

      paint.Print(2, 27, "Elec Day: ");
      paint.Print(2, 37, "Elec Night");
      paint.Print(2, 46, "Water");
      paint.Print(2, 55, "Gas");
      DisplayFlush();
    }

    static void updatescreen1()
    {
      paint.PrintSmall(2, 16, TimeString(PandaDataLogger.Program.uptime));

      paint.PrintRightAlignedClear(125, 27, CounterCollection.EDCounter.ToString("F2"));
      paint.PrintRightAligned(126, 27, CounterCollection.EDCounter.ToString("F2"));
      paint.PrintRightAlignedClear(126, 37, CounterCollection.ENCounter.ToString("F2"));
      paint.PrintRightAligned(126, 37, CounterCollection.ENCounter.ToString("F2"));
      paint.PrintRightAlignedClear(126, 46, CounterCollection.WACounter.ToString("F2"));
      paint.PrintRightAligned(126, 46, CounterCollection.WACounter.ToString("F2"));
      paint.PrintRightAlignedClear(126, 55, CounterCollection.GACounter.ToString("F2"));
      paint.PrintRightAligned(126, 55, CounterCollection.GACounter.ToString("F2"));
      DisplayFlush();
    }

    /// <summary>
    /// Screen 1
    /// </summary>
    static void screen2()
    {
      paint.Clear();
      paint.RectSE(0, 0, 127, 63);
      paint.Line(0, 14, 127, 14);

      //Title
      paint.PrintCentered(0, 3, "e-Home Consumption");

      paint.PrintSmallRightAligned(125, 16, CounterCollection.FreeMem.ToString());

      paint.PrintSmall(2, 27, "Elec Rate: ");
      paint.Print(2, 37, "Elec (Watt)");
      paint.Print(2, 46, "Water (L/min)");
      paint.Print(2, 55, "Gas (m3/h)");
      DisplayFlush();
    }

    static void updatescreen2()
    {
      if (timerenabled)
      {

        // Time
        paint.PrintSmall(2, 16, TimeString(PandaDataLogger.Program.uptime));

        paint.PrintSmallRightAlignedClear(126, 27, CounterCollection.HighRate ? "Day" : "Night");
        paint.PrintSmallRightAligned(126, 27, CounterCollection.HighRate ? "Day" : "Night");

        //Consumption Data

        paint.PrintRightAlignedClear(126, 37, CounterCollection.EDCons.ToString("F0"));
        paint.PrintRightAligned(126, 37, CounterCollection.EDCons.ToString("F0"));
        paint.PrintRightAlignedClear(126, 46, CounterCollection.WACons.ToString("F0"));
        paint.PrintRightAligned(126, 46, CounterCollection.WACons.ToString("F0"));
        paint.PrintRightAlignedClear(126, 55, CounterCollection.GACons.ToString("F0"));
        paint.PrintRightAligned(126, 55, CounterCollection.GACons.ToString("F0"));
        DisplayFlush();
      }
    }

    #endregion

    #region Screen Helpers
    static void DisplayFlush()
    {
      FEZ_Extensions.Large128x64Display.Flush(paint.vram);
    }

    static void DisplayClear()
    {
      paint.Clear();
      FEZ_Extensions.Large128x64Display.Flush(paint.vram);
    }

    static string td(string x)
    {
      return x.Length < 2 ? "0" + x : x;
    }

    static string DateString(DateTime dt)
    {
      return td(dt.Day.ToString()) + "/" + td(dt.Month.ToString()) + "/" + dt.Year;    
    }

    static string TimeString(DateTime dt)
    {
      return td(dt.Hour.ToString()) + ":" + td(dt.Minute.ToString()) + ":" + td(dt.Second.ToString());
    }

    static string TimeString(TimeSpan dt)
    {
      return dt.Days.ToString() + " " + td(dt.Hours.ToString()) + ":" + td(dt.Minutes.ToString()) + ":" + td(dt.Seconds.ToString());
    }

    #endregion

  }
}
